function Post(cfg) {
    this.purl = path + cfg.purl;
    this.posturl = path + cfg.posturl;
    this.subject = $('#' + cfg.subject);
    this.areap = $('#' + cfg.area.p);//地区的省份
    this.areac = $('#' + cfg.area.c);//地区的市
    this.aread = $('#' + cfg.area.d);//地区的区
    this.areaInfo = {};
    this.way = $('#' + cfg.way);// 辅导方式
    this.tp=$('#'+cfg.tp);//教师类型
    this.schedule = $('#' + cfg.schedule); //上课时间
    this.school = $('#' + cfg.school);//所在高中（对教师适用)对于school的显示，需要判断当前的用户是教师还是学生，如果用户
    //没有填写这些信息，提示用户必须先完成这些信息以后再进入这个页面进行操作,为了方便起见，当前用户的类型在用户登录的时候存放到cookie里面
    this.schoolText = $('#' + cfg.schoolText);
    this.init(cfg);
}
Post.prototype = (function() {
    var bindCities = function(url, provinceId) {

    };
    var request = function(url, data, cb) {
        //TODO 这里以后需要加入ajax超时的处理
        $.ajax({
            url:url,
            dataType:'json',
            data:data,
            type:'POST',
            success:function(json) {
                cb(json);
            }
        });
    }
    return {
        init:function(cfg) {
            var _self = this;
            this.btnChooseSchool = $('#' + cfg.csbtn);//选择学校按钮
            this.btnChooseSchool.click(function() {
                _self.selSchool();
            });
            this.areap.change(function() {
                _self.selArea();
            });
            this.postBtn = $('#' + cfg.post);
            this.postBtn.click(function() {
                _self.post();
            });
            this.initProvince();
        },
        initProvince:function() {
            var _self = this;
            request(this.purl, {tp:0}, function(areas) {
                for (var i = 0; i < areas.length; i++) {
                    $('<option />').attr('value', areas[i].c).text(areas[i].n).appendTo(_self.areap);
                }
            });
        },
        post:function() {
            var data = {};
            var _self = this;
            data.subid = _self.subject.val();
            data.area = this.areaInfo.p + ':' + this.areaInfo.c + (this.areaInfo.d ? ':' + this.areaInfo.d : '');
            data.way = this.way.val();
            if (this.schedule.val()) {
                data.schedule = '[' + this.schedule.val().join(',') + ']';
            }
            data.school = this.school.val();
            data.tp=this.tp.val();
            request(_self.posturl, data, function(data) {
                alert(data);
            });
            //alert(this.areaInfo.p+' ' + this.areaInfo.c + ' ' + this.areaInfo.d);
        },
        selSchool:function() {
            var _self = this;
            var tp = parseInt($('#tp').val());
            switch (tp) {
                case 0:// 在校大学生
                    if (uw) {
                        uw.show();
                    } else {
                        uw = new UnivWin(function(a) {
                            a = $(a);
                            $('#school_text').val(a.text());
                            $('#school').val(a.attr('id'));
                            uw.hide();
                        });
                    }
                    break;
                case 1://在职高中教师
                    if (hw) {
                        hw.show();
                    } else {
                        hw = new HighWin(function(a) {
                            $('#school_text').val(a.text());
                            $('#school').val(a.attr('id'));
                            hw.hide();
                        });
                    }
                    break;
                case 3://在职初中教师
                    if (jw) {
                        jw.show();
                    } else {
                        jw = new JuniorWin(function(a) {
                            $('#school_text').val(a.text());
                            $('#school').val(a.attr('id'));
                            jw.hide();
                        });
                    }
            }
        },
        selArea:function() {
            var _self = this;
            var value = _self.areap.val();

            _self.aread.text('');
            if (value != '') {
                _self.areaInfo.p = value;
                delete _self.areaInfo.c;
                delete _self.areaInfo.d;
                request(this.purl, {tp:1, pid:value}, function(areas) {
                    var sel = $('<select/>');
                    $('<option value="">请选择</option>').appendTo(sel);
                    for (var i = 0; i < areas.length; i++) {
                        $('<option />').attr('value', areas[i].c).text(areas[i].n).appendTo(sel);
                    }
                    _self.areac.text('');
                    sel.appendTo(_self.areac);
                    sel.change(function() {
                        _self.selCity();
                    });
                });
            } else {
                delete _self.areaInfo.p;
                delete _self.areaInfo.c;
                delete _self.areaInfo.d;
                _self.areac.text('');
            }
        },
        selCity:function() {
            var _self = this;
            var cityV = this.areac.find('select').val();
            if (cityV != '') {
                _self.areaInfo.c = cityV;
                delete _self.areaInfo.d;
                request(this.purl, {tp:2, pid:cityV}, function(areas) {
                    var sel = $('<select/>');
                    sel.change(function() {
                        var v = $(this).val();
                        if (v != '') {
                            _self.areaInfo.d = v;
                        } else {
                            delete _self.areaInfo.d;
                        }
                    });
                    $('<option value="">请选择</option>').appendTo(sel);
                    if (areas.length > 0) {
                        for (var i = 0; i < areas.length; i++) {
                            $('<option />').attr('value', areas[i].c).text(areas[i].n).appendTo(sel);
                        }
                        _self.aread.text('');
                        sel.appendTo(_self.aread);
                    }
                });
            } else {
                delete _self.areaInfo.c;
                delete _self.areaInfo.d;
                _self.aread.text('');
            }
            //var value = _self.areap.val();
        }
    };
})();

var uw,jw,hw,courseW;
$(document).ready(function() {
    var post = new Post({subject:'subid',tp:'tp',area:{p:'area_p',c:'area_c',d:'area_d'},way:'way',schedule:'schedule',
        school:'school',schoolText:'schoolText', csbtn:'csbtn',post:'post',purl:'/area/get',posturl:'/tutorinfo/save'});
    var teachWay = $('#way');
    teachWay.text('');
    for(var w in TeachWay) {
        $('<option/>').val(w).text(TeachWay[w]).appendTo(teachWay);
    }
    delete teachWay;

    /*
    var schedule = $('#schedule');
    schedule.text('');
    for(var s in Schedule) {
        $('<option/>').val(s).text(Schedule[s]).appendTo(schedule);
    }
    delete schedule;
    */
    SelectSchedule('schedule');

    $('#chooseCourse').click(function(){
      if(courseW){
        courseW.show();
      } else {
        courseW = new Subject(function(a){
          $('#subid').val(a.attr('id'));
          $('#subname').val(a.text());
          courseW.hide();
        });
      }
    });
});